package com.su.test;


import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        int initArr[] = new int[9];
        int targetArr[];
        NumCode numCode = new NumCode();
        List<Integer> list = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8);


        FileOutputStream fileOutputStream = new FileOutputStream("sample.xlsx");

        //创建Excel对象
        XSSFWorkbook workbook = new XSSFWorkbook();
        int curRow = 0;  //记录当前行

        //创建工作表Sheet对象
        XSSFSheet sheet = workbook.createSheet();
        XSSFRow row = sheet.createRow(curRow);


        String data[] = {"初始数码","目标数码","算法类型","实际步数","最佳步数","耗时(单位:微秒)"};
        XSSFCell cell;
        for (int i = 0; i < data.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(data[i]);

        }

        String algorithmTypes[] = {"BFS","DFS","A算法","A*算法"};//记录算法类型
        Strategy strategys[] = {new BFS(),new DFS(),new Aalgorithm(),new SuperAalgorithm()};
        int colIndex;

        for (int r = 0; r < 10; r++) {
            for(int i=15;i<=20;i++){
                Collections.shuffle(list);
                for (int z = 0; z < list.size(); z++) {
                    initArr[z] = list.get(z);
                }
                numCode.setInitArr(initArr);
                targetArr = numCode.generateTargetArr(i);
                numCode.setTargetArr(targetArr);
                for(int k=0;k<algorithmTypes.length;k++){
                    numCode.setStrategy(strategys[k]);
                    numCode.run();
                    row = sheet.createRow(++curRow);
                    colIndex = 0;
                    cell = row.createCell(colIndex++);
                    cell.setCellValue(Arrays.toString(initArr));
                    cell = row.createCell(colIndex++);
                    cell.setCellValue(Arrays.toString(targetArr));
                    cell = row.createCell(colIndex++);
                    cell.setCellValue(algorithmTypes[k]);
                    cell = row.createCell(colIndex++);
                    cell.setCellValue(numCode.getSteps());
                    cell = row.createCell(colIndex++);
                    cell.setCellValue(numCode.getBestSteps());
                    cell = row.createCell(colIndex++);
                    cell.setCellValue(numCode.getTime());
                }
            }
        }



        workbook.write(fileOutputStream);
        fileOutputStream.close();



/*        int initArr[] = {2,0,3,1,8,4,7,6,5};
//        int targetArr[] = {1,8,2,7,5,3,6,0,4};
        NumCode numCode = new NumCode();
        try{
            numCode.setInitArr(initArr);
            int targetArr[] = numCode.generateTargetArr(15);
            numCode.setTargetArr(targetArr);
            numCode.setStrategy(new BFS());
            numCode.run();
            System.out.println("步数:"+numCode.getSteps());
            System.out.println("耗时:"+numCode.getTime());
            System.out.println("最佳步数:"+numCode.getBestSteps());
        }catch (Exception e){
            System.out.println(e.getMessage());
        }*/


/*    有解，但堆内存会跑炸
      try{
            initArr = new int[]{1,2,3,4,5,6,7,8,0};
            targetArr = new int[]{2,3,4,8,0,5,1,6,7};
            numCode.setInitArr(initArr);
            numCode.setTargetArr(targetArr);
            numCode.show();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }*/

/*        int initArr[] = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};
        int targetArr[] = new int[]{1,3,7,4,5,2,0,8,9,6,10,11,13,14,15,12};
        NumCode numCode = new NumCode();
        numCode.setRowAndCol(4,4);
        try{
            numCode.setInitArr(initArr);
            numCode.setTargetArr(targetArr);
            numCode.setStrategy(new Aalgorithm());
            numCode.run();
            System.out.println("耗时:"+numCode.getTime());
            System.out.println("步数:"+numCode.getSteps()                                             );
            numCode.show();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }*/

/*        NumCode numCode = new NumCode();
        List<Integer> list = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8);
        Collections.shuffle(list);
        for (int i = 0; i < list.size(); i++) {
            initArr[i] = list.get(i);
        }
        Collections.shuffle(list);
        for (int i = 0; i < list.size(); i++) {
            targetArr[i] = list.get(i);
        }
        numCode.setInitArr(initArr);
        numCode.setTargetArr(targetArr);
        numCode.show();*/

/*        try{
            int initArr[] = {1,2,3,4,5,6,7,8,0};
            int targetArr[] = {2,3,4,8,0,5,1,6,7};

            NumCode numCode = new NumCode(initArr,targetArr);
            numCode.show();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }*/

/*        try{
            int initArr[] = {1,2,3,4,5,6,7,8,9,10,11,0};
            int targetArr[] = {1,6,2,4,5,0,3,7,9,10,11,8};

            NumCode numCode = new NumCode(3,4,initArr,targetArr);
            numCode.show();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        int initArr[] = {2,8,3,1,0,4,7,6,5};
        int targetArr[] = {1,2,3,8,0,4,7,6,5};

        int initArr[] = {2,8,3,1,4,7,6,5,0};
        int targetArr[] = {0,2,8,3,1,4,7,6,5};

        */



    }


/*    初始数码：
            2  6  1
            0  4  3
            7  8  5
    目标数码：
            1  5  7
            0  8  6
            3  4  2
     初始数码：
      8  3  6
      5  2  0
      7  1  4
    目标数码：
      7  3  0
      4  5  1
      6  8  2

        初始数码：
      2  4  1
      3  0  8
      6  7  5
    目标数码：
      0  4  2
      7  8  5
      6  1  3

      初始数码：
  7  3  0
  4  5  6
  8  1  2
目标数码：
  1  3  8
  4  6  5
  2  7  0



            */

}
